package org.pomirkovany.model;


import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;



import org.pomirkovany.beans.Category;
import org.pomirkovany.beans.Item;

public class DataAccess {
	private String dbURL = "";
	  private String dbUserName = "";
	  private String dbPassword = "";

	  public void setDbURL(String dbURL) {
	    this.dbURL = dbURL;
	    }
	  public String getDbURL() {
	    return dbURL;
	    }

	  public void setDbUserName(String dbUserName) {
	    this.dbUserName = dbUserName;
	    }
	  public String getDbUserName() {
	    return dbUserName;
	    }

	  public void setDbPassword(String dbPassword) {
	    this.dbPassword = dbPassword;
	    }
	  public String getDbPassword() {
	    return dbPassword;
	    }

	  public Connection getConnection() {
	    Connection conn = null;
	    try {
	      conn = DriverManager.getConnection(getDbURL(), getDbUserName(),
	          getDbPassword());
	      }
	    catch (SQLException e) {
	      System.out.println("Could not connect to DB: " + e.getMessage());
	      }
	    return conn;
	    }
	  public void putConnection(Connection conn) {
	    if (conn != null) {
	      try { conn.close(); }
	      catch (SQLException e) { }
	      }
	    }
	  
	  public String getCategoryName(String categoryID) {
		    Category category = CategoryPeer.getCategoryById(this, categoryID);
		    return (category == null) ? null : category.getName();
		    }

		  public Hashtable getCategories() {
		    return CategoryPeer.getAllCategories(this);
		    }

		  public Enumeration getCatIDs() {
		    return CategoryPeer.getAllCategories(this).keys();
		    }
		 
		  public ArrayList getItemsInCategory(String categoryID) {
			    return ItemPeer.getItemsByCategory(this, categoryID);
			    }
		  public Item getItemDetails(String itemID) {
			    return ItemPeer.getItemById(this, itemID);
			    }	
		  public void deleteItemByID(String itemId){
			   ItemPeer.geleteItemById(this,itemId);
		  }
		  public void addItem(Item item){
			  ItemPeer.addItem(this, item);
		  }
		  public void editItem(Item item, String id){
			  ItemPeer.editItem(this, item, id);
			 
		  }
		  public ArrayList search(String keyword){
			  return ItemPeer.searchItems(this, keyword);
		  }
		  public int getCategoryId(String categoryName){
			  return CategoryPeer.getCategoryId(this, categoryName);
		  }
		  
		  public void addCategory(String categoryName){
			  CategoryPeer.addCategory(this, categoryName);
		  }
		 
		
		  
		  
	  
}
